package familytree.yu.com.sso.enums;

import familytree.yu.com.sso.constants.Constants;
import familytree.yu.com.sso.exception.SsoException;

import java.util.Objects;

/**
 * @Author: yuyunbo
 * @Date: 2021/2/22 22:34
 * @Description:
 */
public interface CodeBaseEnum {

    /**
     * 获取枚举编码
     * @return
     */
    int code();

    /**
     * 获取编码对应的描述
     * @return
     */
    String msg();

    static <E extends Enum<?> & CodeBaseEnum> E getEnumByCode(Class<E> enumClass, int code) {
        E[] enumConstants = enumClass.getEnumConstants();
        Enum[] enums = enumConstants;
        int length = enumConstants.length;

        for (int i = 0; i < length; ++i) {
            E e = (E) enums[i];
            if (e.code() == code) {
                return e;
            }
        }

        throw new SsoException(Constants.ENUM_NOT_FOUND, Constants.ENUM_NOT_FOUND_MESSAGE);
    }

    static <E extends Enum<?> & CodeBaseEnum> E getEnumByCode(Class<E> enumClass, String msg) {
        E[] enumConstants = enumClass.getEnumConstants();
        Enum[] var3 = enumConstants;
        int var4 = enumConstants.length;

        for (int var5 = 0; var5 < var4; ++var5) {
            E e = (E) var3[var5];
            if (Objects.equals(e.name(), msg)) {
                return e;
            }
        }

        throw new SsoException(Constants.ENUM_NOT_FOUND, Constants.ENUM_NOT_FOUND_MESSAGE);
    }
}
